/* -----------------------------------------------------------------------
 * Include the following lines in your makefile:
 *
 * unittest3: unittest3.c dominion.o rngs.o
 *      gcc -o unittest3 -g  unittest3.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int main() {
    int i, r, p, w, player, handCount, winner;
    int maxHandCount = 5;
    int seed = 1000;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;
    
    // create arrays of player hands; limited to the max number of players
    // the last player always has the highest score
    int players[MAX_PLAYERS];
	int p0[MAX_HAND];
	int p1[MAX_HAND];
	int p2[MAX_HAND];
	int p3[MAX_HAND];

	for (i = 0; i < MAX_HAND; i++)
    {
        p0[i] = curse;
		p1[i] = estate;
        p2[i] = duchy;
        p3[i] = province;
    }

    printf ("TESTING getWinners():\n");
 
	for (p = 2; p <= MAX_PLAYERS; p++)
    {
       	for (handCount = 1; handCount <= maxHandCount; handCount++)
		{
#if (NOISY_TEST == 1)
			printf("Test game winner with %d players and %d cards in hand.\n", p, handCount);
#endif
			memset(&G, 23, sizeof(struct gameState));   // clear the game state
			r = initializeGame(p, k, seed, &G); // initialize a new game
		
			for (player = 0; player < p; player++)
			{
				G.handCount[player] = handCount;    // set the number of cards on hand
				switch (player)
				{
					case 0:
						memcpy(G.hand[player], p0, sizeof(int) * handCount); // set all the cards to p0 cards
						break;
					case 1:
						memcpy(G.hand[player], p1, sizeof(int) * handCount); // set all the cards to p1 cards
						break;
					case 2:
						memcpy(G.hand[player], p2, sizeof(int) * handCount); // set all the cards to p2 cards
						break;
					case 3:
						memcpy(G.hand[player], p3, sizeof(int) * handCount); // set all the cards to p3 cards
						break;
					default:
						break;
				}
			
#if (NOISY_TEST == 1)
       			printf("Score for player %d = %d\n", player, scoreFor(player,&G));
#endif
       		}
       		
       		//get the winner of the game
       		r = getWinners(players,&G);
       		
       		for(w = 0; w < MAX_PLAYERS; w++)
       		{
       			if(players[w] == 1)
       				winner = w;
       		}
       		
#if (NOISY_TEST == 1)
       		printf("Winner is = %d, expected = %d\n", winner, player - 1);
#endif
       		assert(winner == player - 1); // check if the winner is the last player
       }
	}

    printf("All domain tests passed!\n");
            
    return 0;
}
